package com.wicket.okrcomponent.biz.service.nb;
 import cn.hutool.core.bean.BeanUtil;
 import com.wicket.okrcomponent.biz.service.dto.common.DataSetsListTwoDto;
 import com.wicket.okrcomponent.biz.service.dto.common.DocumentDto;
 import com.wicket.okrcomponent.common.exception.BizException;
 import org.springframework.stereotype.Service;

import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import com.wicket.okrcomponent.biz.service.dto.req.ObtainReceiveFieldsReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.ObtainReceiveFieldsRespDto;
import com.wicket.okrcomponent.biz.service.dto.req.ImplementFileClearReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.ImplementFileClearRespDto;
import com.wicket.okrcomponent.biz.service.dto.req.ImplementTenReceivingFieldReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.ImplementTenReceivingFieldRespDto;
import com.wicket.okrcomponent.biz.service.dto.req.ImplementDocDataSetMergeFieldReqDto;
import com.wicket.okrcomponent.biz.service.dto.res.ImplementDocDataSetMergeFieldRespDto;

 import java.io.File;

/**
 * @author ： 手工接入方法
 * @since 2022/5/28 15:32
 *
 *
 * @version 1.0
 */
@Service
public class NbUnstructuredData {
/**
      * code:receptionService
      * name:M2-获取接收字段
      * desc:undefined
      * gen by moon at 9/18/2022, 8:14:08 PM
      * 
      **/
     @Trace(operationName = "M2-获取接收字段")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ObtainReceiveFieldsRespDto obtainReceiveFields(ObtainReceiveFieldsReqDto reqDto) {
        return BeanUtil.toBean(reqDto,ObtainReceiveFieldsRespDto.class);
      }

    @Trace(operationName = "执行磁盘删除")
    @Tags({@Tag(key = "参数",value = "arg[0]"),
            @Tag(key = "返回值",value = "returnedObj")})
      public void excuteDelFile(String filePath){


        // 创建一个File对象
        File file = new File(filePath);
        if(!file.exists()){
            return;
        }

        // 调用delete方法尝试删除文件
        boolean result = file.delete();

        // 检查文件是否成功被删除
        if (result) {

        } else {
            throw new BizException("-1","文件删除失败",false);
        }
      }
/**
      * code:clearFile
      * name:M2执行文件清理(特殊方法）
      * desc:undefined
      * gen by moon at 1/25/2024, 9:46:36 PM
      *
      **/
     @Trace(operationName = "M2执行文件清理(特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ImplementFileClearRespDto implementFileClear(ImplementFileClearReqDto reqDto) {
       // TODO ruizhe skai dong ; 1/25/2024, 9:46:36 PM
         String rootPath = reqDto.getFileStorageRootPath();
         String relPath = reqDto.getFileRelativePathAccess();
         if(reqDto.getFileStorageRootPath()!=null && reqDto.getFileStorageRootPath().endsWith("/")){
             rootPath = rootPath.substring(0, rootPath.length() - 1);
         }

         if(reqDto.getFileRelativePathAccess()!=null && !reqDto.getFileRelativePathAccess().startsWith("/")){
             relPath = "/" + relPath;
         }

         if(rootPath==null || rootPath.isEmpty() || relPath==null || relPath.isEmpty()){
             throw new BizException("-1","文件根路径或者文件相对路径为空",false);
         }
         String targetFilePath = rootPath + relPath;
         excuteDelFile(targetFilePath);
        return new ImplementFileClearRespDto();
      }
/**
      * code:receptionService
      * name:M2-接收文件夹 ID
      * desc:undefined
      * gen by moon at 3/27/2024, 1:00:17 AM
      *
      **/
     @Trace(operationName = "M2-接收文件夹 ID")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ImplementTenReceivingFieldRespDto implementTenReceivingField(ImplementTenReceivingFieldReqDto reqDto) {
         return BeanUtil.toBean(reqDto,ImplementTenReceivingFieldRespDto.class);
      }
/**
      * code:docDataSetMergeField
      * name:M2执行文档数据集交集合并字段（特殊方法）
      * desc:undefined
      * gen by moon at 3/27/2024, 1:02:51 AM
      *
      **/
     @Trace(operationName = "M2执行文档数据集交集合并字段（特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public ImplementDocDataSetMergeFieldRespDto implementDocDataSetMergeField(ImplementDocDataSetMergeFieldReqDto reqDto) {
         ImplementDocDataSetMergeFieldRespDto retData = new ImplementDocDataSetMergeFieldRespDto();
         for(DataSetsListTwoDto one : reqDto.getDataSetsListTwo()){
             if(reqDto.getDataSetsListOne().contains(one.getUnstructuredDataId())){
                 DocumentDto elm = new DocumentDto();
                 elm.setUnstructuredDataId(one.getUnstructuredDataId());
                 elm.setSourceRelationshipId(one.getSourceRelationshipId());
                 elm.setRelationshipType(one.getRelationshipType());
                 elm.setIsNetworkReference(one.getIsNetworkReference());
                 elm.setPostfix(one.getPostfix());
                 elm.setFileRelativePathAccess(one.getFileRelativePathAccess());
                 retData.getDocumentList().add(elm);
             }
         }
         return retData;
      }
    //手工接入方法
}
